package com.steven.net;

/**
 * Created with IntelliJ IDEA.
 * User: think
 * Date: 12-7-17
 * Time: 下午2:28
 * To change this template use File | Settings | File Templates.
 */


import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class htmlRequest {

    public static void main(String[] args) throws Exception
    {
        URL url = new URL("http://tuan.360buy.com/beijing-0-0-0-0-0-0-1-0-0.html");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(5 * 1000);
        InputStream inStream =  conn.getInputStream();//通过输入流获取html数据
        byte[] data = readInputStream(inStream);//得到html的二进制数据
        System.out.println(data.length);
        String html = new String(data,"utf-8");
        System.out.println(html);


    }
    public static byte[] readInputStream(InputStream instream) throws Exception
    {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[]  buffer = new byte[1204];
        int len = 0;
        while ((len = instream.read(buffer)) != -1)
        {
            outStream.write(buffer,0,len);
        }
        instream.close();
        return outStream.toByteArray();
    }

}

